package web.admin.user;


import domain.User;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.admin.AdminFacade;
import web.admin.components.ActionPanel;
import web.admin.components.CheckBoxPanel;
import web.admin.components.SortableUserDataProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class provides administration of users
 */
public final class UserAdminPanel extends Panel {

    @SpringBean
    AdminFacade adminFacade;
    private Panel empty = new EmptyPanel("current");
    private Panel current = empty;
    private static Logger logger = Logger.getLogger(UserAdminPanel.class.getName());
    private AjaxFallbackDefaultDataTable<User> dataTable;

    public UserAdminPanel(String id) {
        super(id);
        current.setOutputMarkupId(true);
        add(current);
        initTable();
        Form form = new Form("form");
        Button addUser = new Button("addUser") {

            @Override
            public void onSubmit() {
                User user = new User();
                try {
                    adminFacade.saveUser(user);
                } catch (Exception e) {
                    logger.log(Level.SEVERE, null, e);
                    error(getLocalizer().getString("system.error", this));
                }
                replacePanel(user.getId());
            }
        };
        form.add(addUser);
        add(form);

    }

    private void initTable() {
        List<IColumn<User>> columns = new ArrayList<IColumn<User>>();
        columns.add(new AbstractColumn<User>(new Model<String>("Actions")) {
            public void populateItem(Item<ICellPopulator<User>> cellItem, String componentId,
                                     IModel<User> model) {
                cellItem.add(new ActionPanel(componentId, model, UserAdminPanel.this) {
                    @Override
                    public void onClick(AjaxRequestTarget target, IModel model, Panel parent) {
                        replacePanel(((User) model.getObject()).getId(), target);
                    }
                });
            }
        });

        columns.add(new PropertyColumn<User>(new Model<String>("ID"), "id", "id"));
        columns.add(new PropertyColumn<User>(new Model<String>("login"), "login", "login"));
        columns.add(new PropertyColumn<User>(new Model<String>("mail"), "mail", "companyEmail"));
        columns.add(new PropertyColumn<User>(new Model<String>(getLocalizer().getString("user.name", this)), "firstName", "firstName"));
        columns.add(new PropertyColumn<User>(new Model<String>(getLocalizer().getString("user.surname", this)), "lastName", "lastName"));
        columns.add(new AbstractColumn<User>(new Model<String>("Enabled")) {
            public void populateItem(Item<ICellPopulator<User>> cellItem, String componentId,
                                     IModel<User> model) {
                cellItem.add(new CheckBoxPanel(componentId, model) {
                    @Override
                    public boolean isChecked(IModel model) {
                        return ((User) model.getObject()).isEnabled();
                    }

                });
            }
        });
        dataTable = new AjaxFallbackDefaultDataTable<User>("table", columns,
                new SortableUserDataProvider(), 8);
        add(dataTable);
    }


    public void replacePanelToEmpty() {
        current.replaceWith(empty);
        current = empty;
    }

    public void detachUsersModel() {
        dataTable.detachModels();
    }

    public void replacePanel(Long id) {
        this.replacePanel(id, null);
    }

    public void replacePanel(Long id, AjaxRequestTarget target) {
        UserPanel userPanel = new UserPanel("current", id, this);
        current.replaceWith(userPanel);
        current = userPanel;
        if (target != null) {
            target.addComponent(current);
        }
    }
}
